// 与数据库打交道
const {
  create,
  update,
  del,
  list,
  count,
  get,
  getByName,
} = require('../model/project')

const {
  Success,
  SysErr,
  ProjectExistsErr,
  ProjectNotExistsErr,
} = require('../response/code')
const { md5 } = require('../util/crypt')
const { randStr } = require('../util/random')

class ProjService {
  async create(ctx) {
    try {
      const data = ctx.request.body
      const sys_user_id = ctx.state.user.id
      const proj = await getByName(data.name, {
        sys_user_id: sys_user_id,
        deleted_at: null,
      })
      if (proj) {
        return ctx.app.emit('error', ProjectExistsErr, ctx)
      }
      data.code = md5(`${randStr(26)}${sys_user_id}`)
      data.sys_user_id = sys_user_id
      const result = await create(data)

      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async update(ctx) {
    try {
      const data = ctx.request.body
      const sys_user_id = ctx.state.user.id
      const where = null // 密码前端加密后再传过来
      // 项目名称重复检查
      const proj = await getByName(data.name, {
        sys_user_id: sys_user_id,
        deleted_at: null,
        id: { not: data.id },
      })
      if (proj) {
        return ctx.app.emit('error', ProjectExistsErr, ctx)
      }
      const result = await update(data.id, data, where)
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async get(ctx) {
    try {
      const query = ctx.request.query
      const sys_user_id = ctx.state.user.id
      // console.log(query.id, '<<<< query')
      const result = await get(query.id, {
        sys_user_id: sys_user_id,
        deleted_at: null,
      })
      if (!result) {
        return ctx.app.emit('error', ProjectNotExistsErr, ctx)
      }

      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async del(ctx) {
    try {
      const body = ctx.request.body
      const sys_user_id = ctx.state.user.id
      // 检查项目是否存在
      const proj = await get(body.id, {
        sys_user_id: sys_user_id,
        deleted_at: null,
      })
      if (!proj) {
        return ctx.app.emit('error', ProjectNotExistsErr, ctx)
      }
      // 存在则删除
      const result = await del(body.id, {
        sys_user_id: sys_user_id,
        deleted_at: null,
      })
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }

  async list(ctx) {
    try {
      const query = ctx.request.query
      const where = {
        deleted_at: {
          equals: null, // 为 null
          // not: {
          //   equals: null,
          // }, // 不为 null
        },
      }
      const data = await list(query.pageNo, query.pageSize, where) // 可以添加第三个参数条件过滤
      const total = await count(where)
      const result = {
        list: data,
        count: total,
      }
      ctx.body = new Success(result).toString()
    } catch (err) {
      return ctx.app.emit('error', new SysErr(err.message, err.stack), ctx)
    }
  }
}
module.exports = new ProjService()
